<?php

namespace backend\controllers;

use Yii;
use common\models\Fenhuigrade\Fenhuigrade;
use common\models\Fenhuigrade\FenhuigradeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use zhongkefu\zhongkefu;
use yii\filters\VerbFilter;
use common\models\adminlog\AdminLog;
use yii\web\UploadedFile;
use kartik\file\FileInput;//图片上传
use zhongkefu\helpers\Utils;

/**
 * FenhuigradeController implements the CRUD actions for Fenhuigrade model.
 */
class FenhuigradeController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Fenhuigrade models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new FenhuigradeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Fenhuigrade model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Fenhuigrade model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Fenhuigrade();

        //上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {
            $adname = Yii::$app->request->post()['Fenhuigrade']['name'];

            $model->label_img = UploadedFile::getInstance($model, 'label_img');
            if($model->label_img){           
                $siteRoot = Yii::$app->params['adimg'];
                $json = Utils::uploadfile($_FILES['Fenhuigrade']['tmp_name']['label_img'],$_FILES['Fenhuigrade']['name']['label_img'],$siteRoot);
                $model->label_img = $json;   
                        
            }

            if($model->save()){
                Adminlog::addlog(yii::$app->user->id, '添加分会 '.$adname);
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                Yii::$app->session->setFlash('warning', Yii::t('app', '图片不能为空'));
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Fenhuigrade model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        //上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {
            $model->label_img = UploadedFile::getInstance($model, 'label_img');
            if($model->label_img){
                $siteRoot = Yii::$app->params['adimg'];
                $json = Utils::uploadfile($_FILES['Fenhuigrade']['tmp_name']['label_img'],$_FILES['Fenhuigrade']['name']['label_img'],$siteRoot);
                $model->label_img = $json;                
            }
             if (!$model->label_img) {
                $new = $this->findModel($id);
                $model->label_img = $new->label_img;
            }
            if($model->save()){
                $adname = Yii::$app->request->post()['Fenhuigrade']['name'];
                Adminlog::addlog(yii::$app->user->id, '修改分会 '.$adname);
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Fenhuigrade model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Fenhuigrade model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Fenhuigrade the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Fenhuigrade::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 启用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStart()
    {
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){
                $model = Fenhuigrade::findOne(['id' => $id]);
                Fenhuigrade::updateAll(['status'=>Fenhuigrade::STATUS_ACTIVE],['id' =>$model->id]);
            }
            return true;
        }
    }

    /**
     * 停用按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionStop()
    {

        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $id){

                $model = Fenhuigrade::findOne(['id' => $id]);
                Fenhuigrade::updateAll(['status'=>Fenhuigrade::STATUS_DELETED],['id' =>$model->id]);
            }
            return true;
        }
    }
    /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
        $artname = "";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
            foreach($ids as $val){
                $model = Fenhuigrade::findOne(['id' => $val]);
                $artname .= $model->name.",";
            }
            $artname = substr($artname,0,-1);
            foreach($ids as $id){
                $model = Fenhuigrade::findOne(['id' => $id]);
                $this->findModel($id)->delete();

            }
            Adminlog::addlog(yii::$app->user->id, '批量删除会员等级 '.$artname);
            return true;
        }
    }
}
